<?php

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\services\annotation\ControllerAnnotation;
use app\common\services\annotation\NodeAnnotation;

//
use support\Request; //请求类
use support\Response; //请求类
use think\facade\Db; //数据库类
use think\App; //初始化
use think\Exception; //异常
/**
 * @ControllerAnnotation(title="chat")
 */
class ChatController extends AdminController
{

    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\Chat();
    }
    
    public function index(Request $request): Response{
        if (!$request->isAjax()) return view('admin/chat/index');
        if ($request->input('selectFields')) {
            return $this->selectList();
        }
        list($page, $limit, $where) = $this->buildTableParams();
        $count = $this->model->where($where)->count();
        $list  = $this->model->where($where)->order($this->order)->page($page, $limit)->select()->toArray();
        foreach($list as &$ls){
            $ls1 = DB::name('user')->where('id',$ls['fid'])->value('nickname');
            if(!$ls1 or empty($ls1)){
                $ls1 = "用户不存在";
            }
            $ls2 = DB::name('user')->where('id',$ls['userid'])->value('nickname');
            if(!$ls2 or empty($ls2)){
                $ls2 = "用户不存在";
            }
            $ls['atob'] = $ls1.'=>'.$ls2;
        }
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
        ];
        return json($data);
    }
    
}
